<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/public/header.jsp"%>
<%
	String isSP = request.getParameter("isSP");
	String status = request.getParameter("status") == null ? "" : request.getParameter("status");
	if("".equals(status)){
		status = "0";
	}
	String gd_id = request.getParameter("gd_id") == null ? "" : request.getParameter("gd_id");
	String gd_content_id = request.getParameter("gd_content_id") == null ? "" : request.getParameter("gd_content_id");
	String gd_code = request.getParameter("gd_code") == null ? "" : request.getParameter("gd_code");
	String gd_name = request.getParameter("gd_name") == null ? "" : request.getParameter("gd_name");
	String xm_code = request.getParameter("xm_code") == null ? "" : request.getParameter("xm_code");
	String xm_name = request.getParameter("xm_name") == null ? "" : request.getParameter("xm_name");
%>
<html>
	<head>
		<title>分包结算</title>
		<script type="text/javascript" src="<%=contextPath %>/public/js/extend.js"></script>
		<style>
			.td1{
				padding-top:10px;
				padding-bottom:5px;
				font-size: 12px;
				background-color: #c0ebd7;
				width:20%;
			}
			.td2{
				padding:5px;
			}
		</style>
	<script type="text/javascript">
		var lastIndex;
		var url = '';
		var submitUrl = '<%=contextPath %>/springMVC/gd/addGDFBBalance';
		$(function(){
			//先判断当前分包有没有结算
			$.ajax({
				type:'POST',
				url:'<%=contextPath %>/springMVC/gd/hasFBBalance?gd_id=<%=gd_id%>',
				async:false,
				success:function(rtjson){
					$.ajax({
						type:'POST',
						async:false,
						url:'<%=contextPath %>/springMVC/gd/getWorkerAndWorkerMoney?gd_id=<%=gd_id%>',
						success:function(rtJson){
							$('#gd_worker_name').val(rtJson.gd_worker_name);
							$('#worker_material_money').val(rtJson.worker_money);//每次都是从材料平衡处获得
							if(rtjson.rtState == '0'){
								url = '<%=contextPath %>/springMVC/gd/listPageFBBalanceByGdId?gd_id=<%=gd_id%>';
								submitUrl = '<%=contextPath %>/springMVC/gd/updateGDFBBalance';
								$('#submitValue').html("重新保存");
							}else{
								url='<%=contextPath %>/springMVC/gd/listPageFBDetail?gd_id=<%=gd_id%>&gd_content_id='+rtJson.gd_content_id;
							}
						}
					});
				}
			});
			
			$('#table').datagrid({
				title:'分包项目明细',
				url:url,
				rownumbers:true,
				singleSelect:true,
				showFooter:true,
				fitColumns:true,
				nowrap:false,
				columns:[[
				    {field:'id',hidden:true},
					{title:'栏目名称',field:'fb_item_name',width:50,align:'center'},
					{title:'预算成本',field:'budget',width:50,align:'center'},
					{title:'预算备注',field:'budget_remark',width:100,align:'center'},
					{title:'实际成本',field:'actual',width:50,align:'center'},
					{title:'实际成本备注',field:'actual_remark',width:100,align:'center'},
					{title:'实际应付',field:'actual_pay',width:50,align:'center'},
					{title:'实际应付备注',field:'actual_pay_remark',width:100,align:'center'}
				]],
				onBeforeLoad:function(){
					$(this).datagrid('rejectChanges');
				},
				onLoadSuccess:function(data){
					showFooter();
				},
				onAfterEdit:function(){
					showFooter();
				},
				onClickCell:function(rowIndex,field,value){
					if (lastIndex != rowIndex){
						$('#table').datagrid('endEdit', lastIndex);
						$('#table').datagrid('beginEdit', rowIndex);
						var ed = $('#table').datagrid('getEditor', {index:rowIndex,field:field});
						if(ed == null){
							ed = $('#table').datagrid('getEditor', {index:rowIndex,field:'actual'});
						}
						$(ed.target).select();
					}
					lastIndex = rowIndex;
				},
				toolbar:[{
					id:'save',
					text:'保存修改',
					disabled:false,
					iconCls:'icon-save',
					handler:function(){
							$('#table').datagrid('acceptChanges');
							lastIndex =-1;
						}
					}],
				pageSize:50,
				pageList : [50,100,150],  
				pagination:true
			});
			
		});
		
		function showFooter(){
			var jsondata=$("#table").datagrid("getData");
			var budget = 0;
			var actual = 0;
			var actual_pay = 0;
			for(var i=0;i<jsondata.total;i++){
				budget += parseFloat(jsondata.rows[i].budget);
				actual += parseFloat(jsondata.rows[i].actual);
				actual_pay += parseFloat(jsondata.rows[i].actual_pay);
			}
			budget = Math.round(budget*Math.pow(10,2))/Math.pow(10,2);
			actual = Math.round(actual*Math.pow(10,2))/Math.pow(10,2);
			actual_pay = Math.round(actual_pay*Math.pow(10,2))/Math.pow(10,2);
			$('#table').datagrid('reloadFooter',[
			                              	{fb_item_name:'合计:',
			                              		budget:budget,
			                              		actual:actual,
			                              		actual_pay: actual_pay 
			                              	 }
			                              ]);
			$('#actual_pay_total').val(actual_pay);
			$('#worker_pay').val(actual_pay+parseFloat($('#worker_material_money').val()));
		}
		
		function addGDMaterialRequest(obj){
			//自动提交数据
			$('#table').datagrid('acceptChanges');
			lastIndex =-1;
			//自动提交数据
			if($('#ff').form('validate')){
				var products="";
				var jsondata=$("#table").datagrid("getData");
				for(var i=0;i<jsondata.total;i++){
					if(products!=""){
						products+=";";
					}
					products+=jsonToStr(jsondata.rows[i]);
					var index = $("#table").datagrid("getRowIndex",jsondata.rows[i])+1;
					if(jsondata.rows[i].actual == null || jsondata.rows[i].actual < 0){
						$.messager.alert('提示','第'+index+'行实际成本填写不正确！');
						return ;
					}
					if(jsondata.rows[i].actual_pay == null || jsondata.rows[i].actual_pay < 0){
						$.messager.alert('提示','第'+index+'行实际应付填写不正确！');
						return ;
					}
				}

				var param = "gd_id=<%=gd_id%>&products="+encodeURIComponent(products)+"&worker_material_money="+$('#worker_material_money').val()+"&actual_pay_total="+$('#actual_pay_total').val()+"&worker_pay="+$('#worker_pay').val();
				$(obj).hide();
				$.ajax({
					type:'POST',
					url:submitUrl,
					data:param,
					dataType:'json',
					success:function(rtJson){
						$.messager.alert("提示",rtJson.rtMsrg);
					}
				});
			}
		}
		function startPrint(obj){
			$(obj).hide();
			window.print();
		}
	</script>
	</head>
	<body>
		<div >
		<form id="ff">
			<div style="text-align: center;">
					<p style="font-weight: bolder;">分包结算</p>
			</div>
			<table  style="border-collapse: collapse;" width="100%" border="1px" bordercolor="#0eb83a">
				<tr>
					<td align="right" class="td1" nowrap>工单编号：</td>
					<td class="td2" nowrap>
						<input  id="gd_code" name="gd_code" readOnly value="<%=gd_code%>" size=20>
					</td>
					<td align="right" class="td1" nowrap>工单名称：</td>
					<td class="td2" nowrap>
						<input  id="gd_name" name="gd_name" readOnly value="<%=gd_name%>" size=40>
					</td>
				</tr>
				<tr>
					<td align="right" class="td1" nowrap>材料扣款：</td>
					<td class="td2" nowrap>
						<input  id="worker_material_money" name="worker_material_money" readOnly size=20>
					</td>
					<td align="right" class="td1" nowrap>分包商：</td>
					<td class="td2" nowrap>
						<input  id="gd_worker_name" name="gd_worker_name" readOnly  size=40>
					</td>
				</tr>
				<tr>
					<td align="right" class="td1" nowrap>应付总额：</td>
					<td class="td2" nowrap>
						<input  id="actual_pay_total" name="actual_pay_total" readOnly size=20>
					</td>
					<td align="right" class="td1" nowrap>实际应付：</td>
					<td class="td2" nowrap>
						<input  id="worker_pay" name="worker_pay" readOnly  size=40>
					</td>
				</tr>
				<tr>
					<td align="right" class="td1" nowrap>
					</td>
					<td class="td2" nowrap colspan='3'>
						<div align="left">
							<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-print'" onclick="startPrint(this);">确认打印</a>
						</div>
					</td>
				</tr>
			</table>
			</form>
			<div id="table" ></div>
		</div>
	</body>
</html>